package com.mashibing.坦克游戏;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * <简述>        启动窗口类
 * <详细描述>
 *
 * @author LiuShanshan
 * @version $Id$
 */
public class TankFrame extends Frame {
    // 创建坦克
    private Trank tank = new Trank(200, 200, null);

    public TankFrame() {
        setSize(1000, 1000);
        setResizable(false);
        setTitle("坦克设计测试");
        setVisible(true);
        //移动上下左右，可以移动坦克
        this.addKeyListener(new MykeyListener());
        // 关闭窗口
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) { // bjmashibing/tank
                System.exit(0);
            }

        });
    }

    // 启动窗口后,自动调用此方法，显示图案
    @Override
    public void paint(Graphics g) {
        tank.paint(g);
        g.fillRect(tank.getX(), tank.getY(), 50, 50);
    }

    // jwt的监听器
    class MykeyListener extends KeyAdapter {

        /**
         * <简述>    按键之前的操作
         * <详细描述>
         *
         * @param e
         * @return void
         * @author Liushanshan
         */
        @Override
        public void keyPressed(KeyEvent e) {
            // 键位
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    tank.setLeft(true);
                    break;
                case KeyEvent.VK_RIGHT:
                    tank.setRight(true);
                    break;
                case KeyEvent.VK_UP:
                    tank.setUp(true);
                    break;
                case KeyEvent.VK_DOWN:
                    tank.setDown(true);
                    break;
            }
            tank.setMainTankDir();
        }

        /**
         * <简述>    按键之后的操作
         * <详细描述>
         *
         * @param e
         * @return void
         * @author Liushanshan
         */
        @Override
        public void keyReleased(KeyEvent e) {
            // 键位
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    tank.setLeft(false);
                    break;
                case KeyEvent.VK_RIGHT:
                    tank.setRight(false);
                    break;
                case KeyEvent.VK_UP:
                    tank.setUp(false);
                    break;
                case KeyEvent.VK_DOWN:
                    tank.setDown(false);
                    break;
            }
            tank.setMainTankDir();
        }



    }


}
